package com.rfqx.controller.customer;

import static com.rfqx.util.StringHelper.nullToEmpty;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rfqx.entity.Customer;
import com.rfqx.service.CustomerService;

/**
 * Servlet implementation class NewCustomerController
 */
@WebServlet("/customer/newCustomer")
public class NewCustomerController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewCustomerController() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request
						, HttpServletResponse response) throws ServletException, IOException {
		
		CustomerService service = new CustomerService();
		
	    Customer customer = service.save ( 
	    						mappingRequestToCustomer (request) );

	    request.setAttribute("customer", customer);
	    request.setAttribute("message", "Success!!!! \\o/");

	    request.getRequestDispatcher("/WEB-INF/jsp/customer/view.jsp").forward(request, response);
	
	}

	private Customer mappingRequestToCustomer(HttpServletRequest request) {
		
		
		Customer customer = new Customer();
		
		customer.setName( 
				nullToEmpty(request.getParameter("customer-name") )  );
		
		customer.setEmail( 
				nullToEmpty(request.getParameter("customer-email") )  );

		customer.setPhone( 
				nullToEmpty(request.getParameter("customer-phone") )  );

		return customer;
	}
	
	
}



